<?php
class IndexAction extends Action
{
	//初始化载入函数
	protected function _initialize()
	{

		$model = new Model();
		$table_arr = array();
		$dbname = 'Tables_in_'.C('DB_NAME');
		$tables = $model->query('SHOW TABLES');
		$dbname = C('DB_NAME');

		foreach($tables as $value)
		{
			//$tbname = str_replace($dbpre,'',$value[$dbname]);
			$table = $value['Tables_in_'.$dbname];
			array_push($table_arr,$table);
		}

		$this->assign('dbtable',$table_arr);

	}


	/**
	 * function return action
	 *
	 * @package index
	 * @access private
	 */
	function returna()
	{
		$model = trim($_REQUEST["dbtable"]);

		$tname = explode("_",$model);
		if(!empty($model))
		{
			$m = M($tname[1]);
			$field = $m->query("SHOW FULL FIELDS FROM ".$model);
		}

		foreach ($field as $value)
		{
			$code .= C('DB_PREFIX').$tname[1].'.'.$value['Field'].',';
		}
		$code .='
		
		';
		foreach ($field as $value)
		{
			//if($value['Field'])
			//$code .="\$data['".$value['Field']."']=\$_POST['".$value['Field']."'];\r\n";
			$code .="\$_POST['".$value['Field']."']='1';\r\n";
		}
		$code.="
			
			";
		foreach ($field as $value)
		{
			
			$code .="\$data['".$value['Field']."']='';\r\n";
			//$code .="\$_POST['".$value['Field']."']='1';\r\n";
		}

		echo nl2br(htmlentities($code,ENT_NOQUOTES,'utf-8'));
	}

	//返回模型信息 
	//TODO:解决管理查询问题
	function returnm()
	{
		//echo 'aaa';exit;
		$model = trim($_REQUEST["dbtable"]);
		$orp = $_REQUEST["orp"];
		//echo $orp; exit;
		//dump($model);
		//exit;
		$tname = explode("_",$model);
		if(!empty($model))
		{
			$m = M($tname[1]);
			$field = $m->query("SHOW FULL FIELDS FROM ".$model);
		}
		$code ='<?php
		class '.ucwords($tname[1]).'Model extends MetaModel
		{
		';

		$code .='

		protected $_validate  = array(
		';

		//输出自动验证信息
		foreach($field as $value)
		{
			$f =$value['Field'];
			if($_POST["c$f"] <> 'none' && $_POST["c$f"] <> 'auto')
			{
				
					$code .="array ('".$value['Field']."','require','".$value['Comment']."不能为空',1,'',$orp),".'
			';
		
			}
		}

//		foreach ($field as $value)
//		{
//			$code .="array ('".$value['Field']."','require','".$value['Comment']."不能为空',1,'',$orp),".'
//			';
//		}

		$code .='
		);
		
	
		protected $_auto = array(
		';

		//设置默认填充的内容
		$itauto = '';
		//输出自动完成信息
		foreach($field as $value)
		{
			$f =$value['Field'];
			if($_POST["c$f"] =='auto')
			{
				$itauto = $this->itauto($f);
				$code .="array ('".$value['Field']."','$itauto',$orp,'function'),".'
			';
		
			}
		}

		$code .='
		
		);';

		//输出VIEW
		$code .='
		//dump view model
		//public $viewFields = array(
		
		//'."'".$tname[1]."'".'=>array(
		//';
		foreach ($field as $value)
		{
			$f =$value['Field'];
			if($_POST["c$f"] =='view')
			{
				$code .="'".$value['Field']."',";
			}
		}
		$code .="'_on'=>'  '".'),
		//);
		}
		?>';
		echo nl2br(htmlentities($code,ENT_NOQUOTES,'utf-8'));
	}

	//返回表单信息
	function returnr() {
		$model = trim($_REQUEST["dbtable"]);
		//dump($model);
		//exit;
		$tname = explode("_",$model);
		if(!empty($model))
		{
			$m = M($tname[1]);
			$field = $m->query("SHOW FULL FIELDS FROM ".$model);
		}
		//初始化
		$code ='';

		//表单生成，获取表单类型
		$tt = $_POST['tt'];
		if(!empty($tt))
		{
			switch ($tt)
			{
				case 'list':
					//需要配置表头和表尾信息
					if($_POST['adminc'] == 'on')
					{
						$code .= '<div class="leftspace">
					<form id="form1" action="__URL__/" method="post">
					';
					} else {
						$code.='front list';
					}
					//输出列表头部信息
					foreach($field as $key=>$value)
					{
						$f =$value['Field'];
						if($_POST["c$f"] =='show')
						{
							if($_POST['adminc'])
							{
								$code .= '<th>';
								$code .=$value['Comment'];
								$code .= '</th>';
							}else {
								$code.='fronts';
							}
						}
					}
					//输出列表行
					$code .='列表行头部';
					foreach($field as $key=>$value)
					{
						$f =$value['Field'];
						if($_POST["c$f"] =='show')
						{
							if($_POST['adminc'])
							{
								//显示后台
								$code .= '<td>';
								$code .='{$vo.'.$value['Field'].'}';
								$code .= '</td>';
							} else {
								//TODO:显示前台
								$code .= 'front';

							}
						}
					}
					if($_POST['adminc'] == 'on')
					{
						$code .= '<td><a href="__URL__/edi_'.$tname[1].'/id/{$le.id}">编辑</a></td></tr></iterate></table>';
					}
					$code .='列表行底部';
					$code .='
					</form>
					</div>';
					break;

				case 'add':
					if($_POST['adminc'] == 'on')
					{
						$code .= '<include file="Public:header" />
<div class="mainarea">
<div class="maininner">
						
					
					<form id="form1" action="__URL__/deal" method="post">
				    <div class="body_content">
		<div class="top_action"><a href="__URL__">返回</a> | <a href="__URL__/do">变更</a></div>
		<table cellspacing="0" cellpadding="0" id="maintable" class="formtable">
					';
					}else {
						$code .='front table info';
					}

					foreach($field as $key=>$value)
					{

						
						$f = $value['Field'];
						//输出控件类型
						if($_POST['adminc'] == 'on')
						{
							if($_POST["c$f"] =='input')
							{
								$code .='<tr><td>'.$value['Comment'].'</td><td>    <input name="'.$value['Field'].'" value="" type="text"  /> </td></tr>';
							} else if($_POST["c$f"] =='select') {
								$code .='<tr><td>'.$value['Comment'].'</td><td>    <select name="'.$value['Field'].'"'.' class="w-100"><option value="0">系统根ID</option><volist name="list" id="svo"><option value="{$svo.id}" >{$svo.fulltitle}</option></volist></select></td></tr>';
							} else if($_POST["c$f"] =='radio'){
								$code .='<tr><td>'.$value['Comment'].'</td><td>    <input type="radio" name="'.$value['Field'].'" value="1" />有 <input type="radio" name="'.$value['Field'].'" value="2" />无</td></tr>';
							} else if($_POST["c$f"] == 'textarea') {
								$code .='<tr><td>'.$value['Comment'].'</td><td>   <textarea id="'.$value['Field'].'" rows="10" cols="40" name="'.$value['Field'].'"></textarea></td></tr>';

							}
						}else {
							//TODO: show front
							if($_POST["c$f"] =='input')
							{
								$code .='<span>'.$value['Comment'].'</span>    <input name="'.$value['Field'].'" value="" type="text"  /> <br />';
							} else if($_POST["c$f"] =='select') {
								$code .='<span>'.$value['Comment'].'</span>    <select name="'.$value['Field'].'"'.' class="w-100"><option value="test">test</option>'.'</select>';
							} else if($_POST["c$f"] =='radio'){
								$code .='<span>'.$value['Comment'].'</span>    <input type="radio" name="'.$value['Field'].'" value="1" />有 <input type="radio" name="'.$value['Field'].'" value="2" />无';
							} else if($_POST["c$f"] == 'textarea') {
								$code .='<span>'.$value['Comment'].'</span>    <textarea id="'.$value['Field'].'" rows="10" cols="40" name="'.$value['Field'].'"></textarea>';
									
							}
						}

						//$code .='</li>';
						//echo '<br />';
					}
					if($_POST['adminc'] == 'on')
					{
						$code .= '
						</tbody>
					</table>
	</div>
	<div class="foot_action">
		<input type="hidden" name="actmodel" value="'.$tname[1].'" />
		<input type="submit" name="submit" value="提交录入" class="submit">
        <input type="reset" name="button" id="button" value="还原重填" class="submit"/>
	</div>
	</form>
</div>
</div>

<div class="side">
<include file="Public:sider" />
</div>
<include file="Public:footer" />';
					}else {
						$code .='front add';
					}

					break;
				case 'edit':
					if($_POST['adminc'] == 'on')
					{
						$code .= '
<include file="Public:header" />
<div class="mainarea">
<div class="maininner">
	<div class="body_content">
		<div class="top_action"><a href="__URL__">返回</a> | <a href="__URL__/do">变更</a></div>
		<form id="form1" action="__URL__/deal" method="post">		   
		<table cellspacing="0" cellpadding="0" id="maintable" class="formtable">
					';
					
					foreach($field as $key=>$value)
					{

						$f = $value['Field'];
						if($_POST['adminc'] == 'on')
						{
							if($_POST["c$f"] =='input')
							{
								if($value['Field'] == 'id')
								{
									$code .='<tr><td>'.$value['Comment'].'</td><td>    <input name="'.$value['Field'].'" value="{$vo.'.$value['Field'].'}" readonly type="text"  /> </td></tr>';
								}else{
									$code .='<tr><th style="width:12em;">'.$value['Comment'].'</th><td>    <input name="'.$value['Field'].'" value="{$vo.'.$value['Field'].'}" type="text"  /> </td></tr>';

								}
							} else if($_POST["c$f"] =='select') {
								$code .='<tr><td>'.$value['Comment'].'</td> <td>   <select name="'.$value['Field'].'"'.' class="w-100"><option value="0">系统根ID</option><volist name="list" id="svo"><option value="{$svo.id}"  <eq name="svo.id" value="$vo[\'parentid\']"> selected </eq> >{$svo.title}</option></volist></select></td></tr>';
							} else if($_POST["c$f"] =='radio'){
								$code .='<tr><td>'.$value['Comment'].'</td> <td>  <input type="radio" name="'.$value['Field'].'" value="1" />有 <input type="radio" name="'.$value['Field'].'" value="2" />无';
							} else if($_POST["c$f"] == 'textarea') {
								$code .='<tr><td>'.$value['Comment'].'</td> <td>    <textarea id="'.$value['Field'].'" rows="8" cols="70" name="'.$value['Field'].'">{$vo.'.$value['Field'].'}</textarea></td></tr>';
									
							}
						}


					}
					
						$code .= '
						</tbody>
					</table>
	</div>
	<div class="foot_action">
		<input type="hidden" name="actm" value="'.$tname[1].'" />
		<input type="hidden" name="id" value="{$vo.id}"/>
		<input type="submit" name="submit" value="提交录入" class="submit">
        <input type="reset" name="button" id="button" value="还原重填" class="submit"/>
	</div>
	</form>
</div>
</div>

<div class="side">
<include file="Public:sider" />
</div>
<include file="Public:footer" />';
					}else{
						$code = '
<form id="form1" action="__URL__/deal" method="post" enctype="multipart/form-data">		   
<table style="clear:both;width:100%">
<tbody>
					';
					
					foreach($field as $key=>$value)
					{

						$f = $value['Field'];

							if($_POST["c$f"] =='input')
							{
								if($value['Field'] == 'id')
								{
									$code .='<tr><td>'.$value['Comment'].'</td><td>   "{$vo.'.$value['Field'].'}"</td></tr>';
								}else{
									$code .='<tr><td>'.$value['Comment'].'</td><td>    <input size="40" name="'.$value['Field'].'" value="{$vo.'.$value['Field'].'}" type="text"  /> </td></tr>';

								}
							} else if($_POST["c$f"] =='select') {
								$code .='<tr><td>'.$value['Comment'].'</td> <td>   <select name="'.$value['Field'].'"'.' class="w-100"><volist name="list" id="svo"><option value="{$svo.id}"  <eq name="svo.id" value="$vo[\'parentid\']"> selected </eq> >{$svo.title}</option></volist></select></td></tr>';
							} else if($_POST["c$f"] =='radio'){
								$code .='<tr><td>'.$value['Comment'].'</td> <td> {$vo.'.$value['Field'].'} </td></tr>';
							} else if($_POST["c$f"] == 'textarea') {
								$code .='<tr><td>'.$value['Comment'].'</td> <td>    <textarea id="'.$value['Field'].'" rows="8" cols="70" name="'.$value['Field'].'">{$vo.'.$value['Field'].'}</textarea></td></tr>';
									
							}
							$code .='
							';
						


					}
					
						$code .= '
						<tr>
						<td><input type="hidden" name="actm" value="'.$tname[1].'" />
		<input type="hidden" name="id" value="{$vo.id}"/></td>
						<td>
						
		<input type="submit" name="submit" value="我提交啦" class="btn-submit">
        </td>
						</tr>
						</tbody>
					</table>
	
	
		
	</form>';
					}
			}
		}
		echo nl2br(htmlentities($code,ENT_NOQUOTES,'utf-8'));
	}



	public function index()
	{
		$model = '';
		$field ='';
		//获取默认生成代码类型
		$acname ='';// Cookie::get('gnaction');


		//获取数据库表
		if(!empty($_REQUEST["mn"]))
		{
			$model = trim($_REQUEST["mn"]);
		}

		if(!empty($model))
		{
			$m = M(msubstr($model,3,count($model)));
			$field = $m->query("SHOW FULL FIELDS FROM ".$model);

		}
		$this->assign('dbname',$model);
		$this->assign('var',$field);

		$this->display();


	}
	
	/**
	 * 自动填充相应的函数
	 */
	public function itauto($field)
	{
		$r = '';
		switch($field)
		{
			case 'createtime':
				$r = 'time';
				break;
			case 'services_id':
				$r = 'ServicesID';
				break;
			case 'user_id':
				$r = 'userID';
				break;
		}
		return $r;
	}
}